package edu.hawaii.webspider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;

/**
* Test cases for WebSpiderExample.java, which check to see if the URL begins
* with http:// and that the URL is not empty. In addition it check that the number
* of links on http://httpunit.org is 26 as test on September 22, 2007. 
*
* @author Michal Zielinski
*/
public class TestWebSpider {
  WebSpiderExample test = new WebSpiderExample("http://hackystat.org");
  String totallinks = "-totallinks";
  String url = "http://hackystat.org";
  String maxPages = "10";
  String mostPopular = "-mostpopular";
  String logging = "-logging";
  
 /**
  * Test URL beginning with http://.
  */
  @Test
  public void testCorrectUrl() {
    String url = test.startUrl;
    assertNotNull("Testing for empty URL", url);
    assertEquals("Testing for char h at positoin 0", 'h', url.charAt(0));
    assertEquals("Testing for char t at position 1", 't', url.charAt(1));
    assertEquals("Testing for char t at position 2", 't', url.charAt(2));
    assertEquals("Testing for char p at position 2", 'p', url.charAt(3));
    assertEquals("Testing for char : at position 2", ':', url.charAt(4));
    assertEquals("Testing for char / at position 2", '/', url.charAt(5));
    assertEquals("Testing for char / at position 2", '/', url.charAt(6));
  }
  
  /**
   * Test the main method.
   * @throws Exception If problems occur.
   */
  @Test 
  public void testMainNormalOperation() throws Exception { 
    String[] threeArguments = new  String[3];
    threeArguments[0] = totallinks;
    threeArguments[1] = url;
    threeArguments[2] = maxPages;
    assertEquals("assert", "10", threeArguments[2]);

    WebSpiderExample.main(threeArguments);
   
    threeArguments[0] = mostPopular;
    WebSpiderExample.main(threeArguments);
    
    String[] zeroArguments = new  String[0];
    WebSpiderExample.main(zeroArguments);
    assertEquals("Testing number of links", 1, test.getNumLinks());
  }
  
  /**
   * Testing logging function.
   * @throws Exception If problems occur.
   */
  @Test
  public void testLogging() throws Exception {
    String[] fourArguments = new  String[4];
    fourArguments[0] = totallinks;
    fourArguments[1] = url;
    fourArguments[2] = maxPages;
    fourArguments[3] = logging;
   
    WebSpiderExample.main(fourArguments);
    assertEquals("Testing for correct URL", url ,test.startUrl);
    fourArguments[0] = mostPopular;
    WebSpiderExample.main(fourArguments);
  }
  
  /**
   * Test the main method.
   * @throws Exception If problems occur.
   */
  @Test
  public void testMainInNormalOperation() throws Exception {   
    String[] threeArguments = new  String[3];
    threeArguments[0] = totallinks;
    threeArguments[1] = url;
    threeArguments[2] = "michal";
    assertEquals("assert", "michal", threeArguments[2]);
     
    WebSpiderExample.main(threeArguments);
    
    threeArguments[2] = "";
    WebSpiderExample.main(threeArguments);
    
    String[] oneArgument = new  String[1];
    oneArgument[0] = totallinks;
    WebSpiderExample.main(oneArgument);
  }
}


